package org.brisling.ethic.service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.domain.RetDomain;
import org.brisling.common.ConstSysParam;
import org.brisling.common.util.StringUtil;
import org.brisling.ethic.domain.GiftDomain;
import org.brisling.ethic.queryService.QueryGiftService;
import org.brisling.ethic.repository.GiftRepo;
import org.brisling.ethic.service.base.BaseEthicService;
import org.brisling.sysman.domain.UploadFiles;
import org.brisling.sysman.queryService.QueryUploadService;
import org.brisling.sysman.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping("/gift")
/**
 * 
 * <p>Title: GiftService</p>
 * <p>Description: 红包礼品退还后台服務service</p>
 * <p>Company: tongking</p>
 * @author jackson wang
 * @since 1.0
 * @version 1.0
 * @date 2017年5月9日 下午5:12:18
 */
public class GiftService extends BaseEthicService<GiftDomain>{
	
	@SuppressWarnings("unused")
	@Autowired
	private GiftRepo baseRepo;
	@Autowired
	private QueryGiftService qryGiftService;
	
	@Autowired
	private QueryUploadService qryUploadService;
	@Autowired
	private UploadService uploadService;
	public GiftService(){
		this.moduleName = ConstSysParam.MODULE_ETHIC_GIFT;
		this.clazz = GiftDomain.class;
	}
	
	/**
	 * 获取业务主页面 
	 * @return
	 */
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return new ModelAndView("/ethic/gift/main");
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return new ModelAndView("/ethic/gift/editDlg");
	}
	
	
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/getListDomain.action")
	public ListDomain<GiftDomain> getRoleListDomain(
			@RequestParam(value="page",required=false) String page,
			@RequestParam(value="rows",required=false) String rows,
			@RequestParam(value="sort",required=false) String sort){
		
				
		return super.getPagedList(page, rows);
			
	}
	
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/getList.action")
	public List<GiftDomain> getRoleList(
			@RequestParam(value="page",required=false) String page,
			@RequestParam(value="rows",required=false) String rows,
			@RequestParam(value="sort",required=false) String sort){
		
		
			return super.getAll(page,rows,getSort(sort));
			
	}
	
	/**
	 * 根据角色名称查找角色集合
	 * @param roleNames
	 * @return
	 */
	public List<GiftDomain> getRoleListByRoleNames(List<String> roleNames){
		
		return super.getListByNames(roleNames);
			
	}
	
	/**
	 * 根据角色名称查找角色编码
	 * @param roleNames
	 * @return
	 */
	public List<String> getRoleIdsByRoleNames(List<String> roleNames){
		
		List<GiftDomain> roles = this.getRoleListByRoleNames(roleNames);
		
		
		return StringUtil.getStringIdList(roles);
	}
	
	/**
	 * 删除记录,单条或批量删除记录,id不为空，执行单条删除，id为空且ids不为空，执行批量删除
	 * @param id	待删除的单条记录id
	 * @param ids	待删除的多条记录id，以','分隔
	 * @return
	 */
	@RequestMapping("/del.action")
	public RetDomain delRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
				
		return super.delRows(id, ids);		 
	}
	
	/**
	 * 删除记录,单条或批量删除记录,id不为空，执行单条删除，id为空且ids不为空，执行批量删除
	 * @param id	待删除的单条记录id
	 * @param ids	待删除的多条记录id，以','分隔
	 * @return
	 */
	@RequestMapping("/flowDel.action")
	public RetDomain delFlowRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
		
			return delRow(id,ids);
		
	}
	
	
	/**
	 * 保存日常考评记录
	 * @param id
	 */
	@RequestMapping("/save.action")
	public GiftDomain save(@RequestBody String body){
		GiftDomain role = bodyToObj(body, GiftDomain.class);
		
		GiftDomain retRole = super.save(role);
		
		if(role.getTmpid()!=null){
			UploadFiles uf =qryUploadService.getOneByField("bizno", role.getTmpid());
			if(uf!=null && uf.getId()!=null){
				uf.setBizno(retRole.getId().toString());
				uploadService.save(uf);
			}
		}
		return retRole;
		 
	}
	
	/**
	 * 批量保存日常考评记录
	 * @param id
	 */
	@RequestMapping("/saveList.action")
	public RetDomain saveList(GiftDomain de){
		RetDomain domain = new RetDomain();
		if(de!=null){
			if(de.getTmpid()!=null){
				
				if(de.getPerson_code()!=null && de.getPerson_name()!=null){
					String[] ids = de.getPerson_code().split(",");
					String[] names = de.getPerson_name() .split(",");
					UploadFiles uf =qryUploadService.getOneByField("bizno", de.getTmpid());
					
					for(int i=0;i<ids.length;i++){
						GiftDomain eu = null;
						try {
							eu = (GiftDomain) BeanUtils.cloneBean(de);
						} catch (IllegalAccessException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (InstantiationException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (InvocationTargetException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (NoSuchMethodException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						eu.setId(null);
						eu.setPerson_code(ids[i]);
						eu.setPerson_name(names[i]);
						eu = super.save(eu);
						
						if(uf!=null && uf.getId()!=null){
							UploadFiles newuf = new UploadFiles();
							newuf.setBizcode(eu.getBizcode());
							newuf.setBizno(eu.getId().toString());
							newuf.setFilename(uf.getFilename());
							newuf.setSavedFileName(uf.getSavedFileName());
							newuf.setFiletype(uf.getFiletype());
							uploadService.save(newuf);
						}
				}
				}
			}
		}
		

		domain.setSuccessMsg("success");
		return domain;
	}
	
	
	/**
	 * 批量审核
	 * @param ids
	 * @param status
	 * @return
	 */
	@RequestMapping("/auditList.action")
	public RetDomain setAuditStatusList(@RequestParam(value="ids",required=false) String ids,@RequestParam(value="status",required=false)String status){
		
		RetDomain domain  = new RetDomain();
		if( (ids!=null) && (status !=null)){
			 int num = super.updateAuditStatusList(ids, status);
			 domain.setSuccessMsg(num+"");
		}else{
			domain.setErrorMsg("error");
		}
		
		return domain;
	}
	
	/**
	 * 单条记录审核
	 * @param id
	 * @param status
	 * @return
	 */
	@RequestMapping("/auditOne.action")
	public GiftDomain setAuditStatus(Long id,String status){
		String ret = null;
		if( (id!=null) && (status !=null)){
			ret =super.updateAuditStatus(id, status);
		}
		if(ret!=null && ret=="200"){
			return qryGiftService.getOne(id);
		}
		return null;
	}
	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/auditPermit.action")
	public GiftDomain auditPermit(@RequestParam(value="id",required=false) Long id){
		if(id!=null){
			return setAuditStatus(id,ConstSysParam.AUDIT_PERMIT);
		}
		return null;
	}
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/auditDenied.action")
	public GiftDomain auditDenied(@RequestParam(value="id",required=false) Long id){
		if(id!=null){
			return setAuditStatus(id,ConstSysParam.AUDIT_DENIED);
		}
		return null;
	}
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/auditSubmit.action")
	public GiftDomain auditRequest(@RequestParam(value="id",required=false) Long id){
		if(id!=null){
			return setAuditStatus(id,ConstSysParam.AUDIT_SUBMIT);
		}
		return null;
	}
	
	
}
